//
//  yueShaoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/13.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "yueShaoViewController.h"
#import "HttpRequest.h"
#import "yueSao01.h"
#import "yueSaoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "yueShaoDetailViewController.h"
#import "yueShaoOrderViewController.h"
//通过判断来确定出跳转到哪个界面
#import "tiaoZhuanView.h"
//附值type类型
#import "yueSaoDetail.h"
#import "Macros.h"
#import "SVProgressHUD.h"

@interface yueShaoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UIView *headerView02;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
//接口地址
@property(nonatomic,copy) NSString *urlString;
//存储网络数组
@property(nonatomic,copy)NSMutableArray *json_Array1;
@property(nonatomic,retain)yueSaoTableViewCell*myCell;
//更改字体(月嫂还是催乳师)
@property (weak, nonatomic) IBOutlet UILabel *changeTitleLabel;
@property(nonatomic,copy)NSDictionary*id_typeDic;
@property(nonatomic,copy)NSMutableArray *id_typeArray;

@end

@implementation yueShaoViewController
@synthesize json_Array1,myCell,urlString,id_typeDic,id_typeArray;
-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    json_Array1=[NSMutableArray array];
    id_typeArray=[NSMutableArray array];
    //设置状态栏的颜色
    [self setHeaderViewColor];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self updateDataFromNet];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.myTableView.hidden=YES;
}
//设置状态栏的颜色
-(void)setHeaderViewColor
{
    //灰色
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
    self.headerView02.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
//     更改确定按钮的边框
//    self.quitButton.clipsToBounds=YES;
//    self.quitButton.layer.cornerRadius=10.0f;
//    self.quitButton.backgroundColor=[UIColor colorWithRed:89/255.0 green:128/255.0 blue:172/255.0 alpha:1];
}
-(void)updateDataFromNet
{
    //判断是在催乳师的界面,还是在月嫂界面
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    if (tzv.yueShao==1)
    {
        self.changeTitleLabel.text=@"月嫂";
        urlString=[NSString stringWithFormat:@"http://112.74.114.1/parenting/index.php/Api/Position/showbs"];
    }else{
       self.changeTitleLabel.text=@"催乳师";
        urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/Api/position/showpl"];
    }
//    NSString *urlString=[NSString stringWithFormat:@"http://112.74.114.1/parenting/index.php/Admin/Position/showbs"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:nil success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_Array1=[json objectForKey:@"data"];
         if (json_Array1.count>0)
         {
             [self dismiss];
         }
//           NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
-(void)dismiss
{
    self.myTableView.hidden=NO;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 73;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return json_Array1.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *str=@"cell";
    myCell=[tableView dequeueReusableCellWithIdentifier:str];
    
    if (myCell==nil)
    {
        myCell=[[[NSBundle mainBundle] loadNibNamed:@"yueSaoTableViewCell" owner:self options:nil]lastObject];
    }
    yueSao01 *ys01=[yueSao01 sharedManager];
    ys01.name=[json_Array1[indexPath.row] objectForKey:@"name"];
    ys01.workage=[json_Array1[indexPath.row] objectForKey:@"workage"];
    ys01.pic=[json_Array1[indexPath.row] objectForKey:@"pic"];
    ys01.idString=[json_Array1[indexPath.row]objectForKey:@"id"];
    //判断是在催乳师的界面,还是在月嫂界面
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    yueSaoDetail *ysd=[yueSaoDetail shardeManager];
    if (tzv.yueShao!=1)
    {
        ysd.type=[json_Array1[indexPath.row]objectForKey:@"type"];
        id_typeDic=[NSDictionary dictionaryWithObject:ysd.type forKey:ys01.idString];
        [id_typeArray addObject:id_typeDic];
    }
    int tagtag=[ys01.idString intValue];
    
    myCell.btn.tag=tagtag;
    myCell.nameLabel.text=[NSString stringWithFormat:@"%@    经验%@年",ys01.name,ys01.workage];
    //更改按钮里的颜色
    myCell.btnLabel.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    //更改确定按钮的边框
    myCell.btnLabel.clipsToBounds=YES;
    myCell.btnLabel.layer.cornerRadius=5.0f;
    [myCell.btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    NSURL* imagePath = [NSURL URLWithString:ys01.pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    return myCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    //点击cell后cell的颜色不变成灰色的
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *idString=[json_Array1[indexPath.row] objectForKey:@"id"];
    yueSao01 * ys01=[yueSao01 sharedManager];
    ys01.idString=idString;
    yueShaoDetailViewController *ysdvc=[yueShaoDetailViewController new];
    [self.navigationController pushViewController:ysdvc animated:YES];
}
- (IBAction)goBack:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)onClick:(UIButton*)sender
{
    //跳转到月嫂订单界面
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    tzv.yueShaoViewNumber=1;
    yueSao01 *ys01=[yueSao01 sharedManager];
    NSString *strstr=[NSString stringWithFormat:@"%ld",sender.tag];
    ys01.idString=strstr;
    yueSaoDetail *ysd=[yueSaoDetail shardeManager];
    if (id_typeArray.count>0)
    {
        for (NSDictionary*dic in id_typeArray)
        {
            NSString *type_type=[dic objectForKey:strstr];
            if (type_type)
            {
                ysd.type=type_type;

            }
        }
        
    }
//    NSLog(@"ysd.typeysd.typeysd.type=%@",ysd.type);
    yueShaoOrderViewController *ysovc=[yueShaoOrderViewController new];
    [self.navigationController pushViewController:ysovc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
